package com.example.uit_management;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ArrayAdapter_Pattern extends BaseAdapter {
	// XML node keys
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;   
    
    public ArrayAdapter_Pattern(Activity a, ArrayList<HashMap<String, String>> d) {    	
        activity = a;        
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }   
    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.pattern_row, null);       
        // Get id of row       
        TextView NienKhoa = (TextView)vi.findViewById(R.id.Semester_pattern); 
        TextView STT = (TextView)vi.findViewById(R.id.STT_TextView_pattern); 
        TextView Ma = (TextView)vi.findViewById(R.id.Ma_TextView_pattern); 
        TextView Ten = (TextView)vi.findViewById(R.id.Ten_TextView_pattern);        
        TextView TongTC = (TextView)vi.findViewById(R.id.TongTC_TextView_pattern);  
        TextView LT = (TextView) vi.findViewById(R.id.LT_TextView_pattern);
        TextView TH = (TextView) vi.findViewById(R.id.TH_TextView_pattern);
        TextView Loai = (TextView) vi.findViewById(R.id.Loai_TextView_pattern);
        TextView YeuCau = (TextView) vi.findViewById(R.id.YeuCau_TextView_pattern);
        HashMap<String, String> array = new HashMap<String, String>();
        array = data.get(position);
        String a= "Bao cao :"+ position;
        // Setting all values in listview
        
        //-- Set Ma mon
        STT.setText(""+ position);
        STT.setBackgroundResource(R.drawable.blue_bar8);
        //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        
        //-- Set Ma mon
        Ma.setText(array.get(JsonParser_pattern.KEY_subjectID));
        Ma.setBackgroundResource(R.drawable.blue_bar8);
        //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        
        //-- Ten Mon
        Ten.setText(array.get(JsonParser_pattern.KEY_subjectName));      
        Ten.setBackgroundResource(R.drawable.blue_bar8);  
        
        //-- TongTC
        TongTC.setText(array.get(JsonParser_pattern.KEY_subjectCredit));
        TongTC.setBackgroundResource(R.drawable.blue_bar8);
        //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        //-- Tinh chi ly thuyet
        LT.setText(array.get(JsonParser_pattern.KEY_subjectTheory));
        LT.setBackgroundResource(R.drawable.blue_bar8);
        //-- Tinh chi thuc hanh
        TH.setText(array.get(JsonParser_pattern.KEY_subjectPractice));
        TH.setBackgroundResource(R.drawable.blue_bar8);
        
        //-- Loai
        Loai.setText(array.get(JsonParser_pattern.KEY_subjectType));
        Loai.setBackgroundResource(R.drawable.blue_bar8);
        
        //-- Yeu cau
        YeuCau.setText(array.get(JsonParser_pattern.KEY_subjectRequired));      
        YeuCau.setBackgroundResource(R.drawable.blue_bar8);      
        
        
        NienKhoa.setText("Nien Khoa");
        //
        NienKhoa.setVisibility(View.GONE);
		NienKhoa.setHeight(0);
		NienKhoa.setBackgroundResource(0);
		
		
		////////////////////////
		// hien hoc ki hoc
		//////////////////////
		// Ap dung cho 1 hoac tat ca hoc ki
        if(Activity_list_pattern.getSemester_Spinner_lop().toString().equals("--Chọn lớp--")|| true){ 
        	Log.e(ArrayAdapter_Pattern.class.toString(), "dung 1");
        	if(position !=0 && position >0){
        		Log.e(Activity_list_pattern.class.toString(), "dung 2");
        		if(!(data.get(position-1).get(JsonParser_pattern.KEY_patternSemester).equals(data.get(position).get(JsonParser_pattern.KEY_patternSemester)))){
        			// thay doi nien khoa
        			Log.e(ArrayAdapter_Pattern.class.toString(), "dung 3.1");
        			// set visible
        			NienKhoa.setVisibility(View.VISIBLE);        			
        			// set text
        			NienKhoa.setText("Học kỳ :" +data.get(position).get(JsonParser_pattern.KEY_patternSemester));
        			//set height        
        			NienKhoa.setHeight(Activity_Score.Height_sore);
        			//NienKhoa.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        			// set backgroud
        			NienKhoa.setBackgroundResource(R.drawable.blue_bar6);
        			
        		}
        	}        	
        	else{
        		// thay doi nien khoa
    			Log.e(ArrayAdapter_Pattern.class.toString(), "dung 3.3: "+Activity_Score.Height_sore);
    			// set visible
    			NienKhoa.setVisibility(View.VISIBLE);        			
    			// set text
    			NienKhoa.setText("Học kỳ :" +data.get(position).get(JsonParser_pattern.KEY_patternSemester));
    			//set height        
    			NienKhoa.setHeight(Activity_Score.Height_sore);
    			// set backgroud
    			NienKhoa.setBackgroundResource(R.drawable.blue_bar6);
    			
        	}        	        
        } 
        return vi;        
    }
	//////////////////////////////
	//danh dau mon hoc da hoc lai
	///////////////////////////////
    /**danh dau mon hoc da hoc lai, hoac rot */
//    void Check_Score_Fail(HashMap<String, String> array , ArrayList<HashMap<String,String>> data, int position){
//
//		Log.e(ArrayAdapterScore.class.toString(), "4");
//		// vong lap
//		for (int i = 0; i < data.size(); i++) {
//		Log.e(ArrayAdapterScore.class.toString(), "4.1");
//		//mon rot hoac chua co diem
//		if(!array.get(JsonParser_regCourse.KEY_scoreFinal).equals("")){			
//			if((Float.parseFloat(array.get(JsonParser_regCourse.KEY_scoreFinal).toString()))<=5){
//				//Subname_score.setHighlightColor(color.black);
//		        Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
//		} 
//		}else{
//		// rot hoac chua co diem
//			Log.e(ArrayAdapterScore.class.toString(),"Black");
//        	Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
//		}
//		if(i!=position){
//		Log.e(ArrayAdapterScore.class.toString(), "dung 6"+i);
//		if(data.get(i).get(JsonParser_regCourse.KEY_subjectID).equals(array.get(JsonParser_regCourse.KEY_subjectID))){
//			Subname_score.setTypeface(null,Typeface.BOLD);
//			classID_core.setTypeface(null,Typeface.BOLD);
//			scorefinal.setTypeface(null,Typeface.BOLD);
//			subjectCredit.setTypeface(null,Typeface.BOLD);        			   
//			break;
//		}else
//			Subname_score.setTypeface(null,Typeface.NORMAL);
//			classID_core.setTypeface(null,Typeface.NORMAL);
//			scorefinal.setTypeface(null,Typeface.NORMAL);
//			subjectCredit.setTypeface(null,Typeface.NORMAL); 
//		}
//		Log.e(ArrayAdapterScore.class.toString(), "dung 7"+i);
//		}	
//    }
}